<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="arer">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{this.currentCity}}</div>
          </div>
        </div>
      </div>
      <div class="arer border-topbottom">
        <div class="title">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper" v-for="item in hotCities" :key="item.id" @click="handleCityClick(item.name)">
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="arer" v-for="(item, key) of cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list">
          <div class="item border-bottom" v-for="innerItem of item" :key="innerItem.id" @click="handleCityClick(innerItem.name)">{{innerItem.name}}</div>
        </div>
        <!-- 运用双层循环，对象里面循环第二个值为key 第一层循环里面的Key值和第二层里面的key可以相同，同一层的key值不可以相同 -->
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'CityList',
  props: {
    cities: Object,
    hotCities: Array,
    letter: String
  },
  computed: {
    ...mapState({
      currentCity: 'city' /* 把vuex里面的city这个公用数据 映射到当前组件的computed计算属性中 映射过来的名字叫做currentCity 之后调用state里面的city直接用this.currentCity来调用 */
    })
  },
  methods: {
    handleCityClick (city) {
      /* alert(city)  通过actions上的dispatch方法调用修改数据this.$store.dispatch('changeCity', city) */
      /* this.$store.commit('changeCity', city)  直接通过mutation上的commit方法调用修改数据 */
      this.changeCity(city)
      this.$router.push('/') /* 编程式导航 */
    },
    ...mapMutations(['changeCity']) /* 有一个mutation叫做changeCity 然后把这个mutation映射到当前组件的名字叫做changeCity的方法里调用这个mutation的时候直接 this.changeCity(数据) */
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  },
  /* 监听letter的变化 */
  watch: {
    letter () {
      console.log(this.letter)
      /* 如果letter不为空 */
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        /* 通过this.letter这个字母获取到这个字母的区域 得到element为数组 */
        console.log(element)
        this.scroll.scrollToElement(element)
      }
    }
  }
}
</script>

<style scoped>
  .list {
    overflow: hidden;
    font-size: .24rem;
    position: absolute;
    top: 1.58rem;
    left: 0;
    right: 0;
    bottom: 0
  }
  .title {
    line-height: .54rem;
    background: #eee;
    padding-left: .2rem;
    color: #666;
    font-size: .26rem;
  }
  .border-topbottom:before {
    border-color: #ccc
  }
  .border-topbottom:after {
    border-color: #ccc
  }
  .button-list {
    overflow: hidden;
    padding: .1rem .6rem .1rem .1rem
  }
  .button-wrapper {
    float: left;
    width: 33.33%
  }
  .button {
    margin: .1rem;
    padding: .1rem 0;
    text-align: center;
    border: .02rem solid #ccc;
    border-radius: .06rem;
  }
  .item-list .item {
    line-height: .76rem;
    padding-left: .2rem;
  }
</style>
